﻿// ***********************************************************************
// Assembly         : OSS.ModelViewer
// Author           : tian_
// Created          : 01-07-2019
//
// Last Modified By : tian_
// Last Modified On : 01-07-2019
// ***********************************************************************
// <copyright file="TestVideoCmd.cs" company="">
//     Copyright ©  2018
// </copyright>
// <summary></summary>
// ***********************************************************************
using System.Linq;
using Osg;
using ZFGK.Addins;
using ZFGK.OSG.UI;
using ZFGK.OSG.UI.Utility;
using ZFGK.OSG.Utility;
using ZFGK.Utility;

namespace OSS.ModelViewer.Commands.TestCmds
{
    /// <summary>
    /// Class TestVideoCmd.
    /// </summary>
    /// <seealso cref="ZFGK.Addins.IExtendCommand" />
    public class TestVideoCmd : IExtendCommand
    {
        /// <summary>
        /// Executes the specified command data.
        /// </summary>
        /// <param name="cmdData">The command data.</param>
        /// <param name="message">The message.</param>
        /// <returns>CmdResult.</returns>
        public CmdResult Execute(ExtendCmdData cmdData, ref string message)
        {
            var viewForm = cmdData.ViewForm as IViewForm;
            var osgView = viewForm.View as IOsgViewCtrl;
            var osgObj = osgView.OsgObj;

//             string filePath = null;
//             if (!DialogUtil.Open("选择贴图文件", "视频(*.mp4;*.avi)|*.mp4;*.avi", out filePath))
//             {
//                 return CmdResult.Cancel;
//             }
//             osgDB.Registry.instance().addFileExtensionAlias("mp4", "ffmpeg");
//             osgDB.Registry.instance().addFileExtensionAlias("avi", "ffmpeg");
//            Image image = osgDB.osgDB.readImageFile(filePath);
                      
            var list = new[]
            {
                "rtsp://user:zfgk1234@192.168.1.64:554/h264/ch1/main/av_stream"
            };

            var readerWriter = OsgDB.Registry.instance().getReaderWriterForExtension("ffmpeg");
            Image image = readerWriter.readImage(list.First()).getImage();

            Geometry geom = new Geometry();
            Vec3Array vertexArray = new Vec3Array();
            float length = 1, width = 2, xOffset = 1, /*yOffsrt = 1,*/ zOffset = 1;
            vertexArray.push_back(new Vec3f(-length + xOffset, -width + zOffset, 0.0f));
            vertexArray.push_back(new Vec3f(length + xOffset, -width + zOffset, 0.0f));
            vertexArray.push_back(new Vec3f(length + xOffset, width + zOffset, 0.0f));
            vertexArray.push_back(new Vec3f(-length + xOffset, width + zOffset, 0.0f));
            geom.setVertexArray(vertexArray);

            //由保存的数据绘制四个顶点的多边形
            geom.addPrimitiveSet(new DrawArrays((uint)PrimitiveSet.Mode.QUADS, 0, 4));

            //为法线创建数组
            Vec3Array normalArray = new Vec3Array();
            normalArray.push_back(new Vec3f(0, 0, 1));
            geom.setNormalArray(normalArray, Osg.Array.Binding.BIND_OVERALL);

            //为颜色创建数组
            Vec4Array colorArray = new Vec4Array();
            colorArray.push_back(new Vec4f(1, 1, 1, 1));
            geom.setColorArray(colorArray, Osg.Array.Binding.BIND_OVERALL);

            //为纹理创建数组
            Vec2Array texCoordArray = new Vec2Array();//创建一个 Vec2Array对象以保存纹理单元 0 的纹理坐标

            texCoordArray.push_back(new Vec2f(0, 0));
            texCoordArray.push_back(new Vec2f(0, 1));
            texCoordArray.push_back(new Vec2f(1, 1));
            texCoordArray.push_back(new Vec2f(1, 0));
            geom.setTexCoordArray(0, texCoordArray);

            //向Geode类添加几何体（Drawable）
            Geode geode = new Geode();
            geode.addDrawable(geom);

            StateSet stateSet = geode.getOrCreateStateSet();
            stateSet.setMode(GL.GL_LIGHTING, (uint)StateAttribute.Values.OFF);
            if (!image.IsValid || !image.valid())
            {
                ZfMessageUtil.ShowError("打开图像文件失败！");
                return CmdResult.Cancel;
            }
            var imageStream = new ImageStream(image);
            if (imageStream.IsValid && imageStream.valid())
            {
                imageStream.play();
            }

            //将图像关联到Texture 2D对象
            Texture2D texture2D = new Texture2D();
            texture2D.setImage(image);
            //texture2D.setTextureHeight();
            // 创建纹理对象后,释放内部的ref_ptr<Image>,删除Image图像。
            texture2D.setUnRefImageDataAfterApply(true);

            texture2D.setResizeNonPowerOfTwoHint(false);
            texture2D.setFilter(Osg.Texture.FilterParameter.MIN_FILTER, Osg.Texture.FilterMode.LINEAR);
            texture2D.setFilter(Osg.Texture.FilterParameter.MAG_FILTER, Osg.Texture.FilterMode.LINEAR);
            texture2D.setWrap(Osg.Texture.WrapParameter.WRAP_S, Osg.Texture.WrapMode.CLAMP_TO_EDGE);
            texture2D.setWrap(Osg.Texture.WrapParameter.WRAP_T, Osg.Texture.WrapMode.CLAMP_TO_EDGE);

            stateSet.setTextureAttributeAndModes(0, texture2D);//关联材质属性与模式到材质单元0

//             var osgNode = new OsgNode("Models");
//             osgNode.AddOrReplaceModel(new NodeData("1") { Node = geode });
//             osgView.AddOrReplaceModel(osgNode);

            osgView.OsgObj.AddOrReplaceModel("Models", geode, "1");

            return CmdResult.Succeed;
        }
    }
}
